package com.xiguaji.tiktok.utils.aes;

import cn.hutool.core.date.DateUtil;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class AmazonElasticsearchServiceSample {

    private static String serviceName = "es";
    private static String region = "us-east-2";
    private static String aesEndpoint = "https://vpc-tiktokes-n2odms5et5strigj75wrbd5hwq.us-east-2.es.amazonaws.com"; // e.g. https://search-mydomain.us-west-1.es.amazonaws.com
    private static String index = "my-index";
    private static String type = "_doc";
    private static String id = "1";
    static  AWSCredentialsProvider credentialsProvider = new MyProvider();

    public static void main(String[] args) throws IOException {


        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String text = date.format(formatter);
        System.out.println(text);

        LocalDateTime localDateTime = DateTimeUtils.localDateTimeBySecond(1591040572);
        String text2 = localDateTime.format(formatter);
        System.out.println(text2);


        LocalDateTime now = LocalDateTime.now();
        LocalDateTime before = LocalDateTime.now().minusDays(90);
        StringBuffer stringBuffer = new StringBuffer();

        while(Integer.parseInt(before.format(DateTimeFormatter.ofPattern("yyyMMdd"))) <= Integer.parseInt(now.format(DateTimeFormatter.ofPattern("yyyMMdd")))){
            stringBuffer.append("aweme_index_"+ DateUtil.format(before,"yyyyMM")).append(",");
            before = before.plusDays(30);
        }
        System.out.println(stringBuffer.toString().substring(0,stringBuffer.toString().length()-1));



        /*RestHighLevelClient esClient = esClient(serviceName, region);

        // Create the document as a hash map
        Map<String, Object> document = new HashMap<>();
        document.put("title", "Walk the Line");
        document.put("director", "James Mangold");
        document.put("year", "2005");
        GetRequest getRequest = new GetRequest("test","type1");
        getRequest.id("1");
        System.out.println("2222222222");
        val getResponse = esClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.toString());*/


        // Form the indexing request, send it, and print the response
        /*IndexRequest request = new IndexRequest(index, type, id).source(document);
        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());*/
    }

    public static void test() throws IOException {
        RestHighLevelClient esClient = esClient(serviceName, region);

        // Create the document as a hash map
        Map<String, Object> document = new HashMap<>();
        document.put("title", "Walk the Line");
        document.put("director", "James Mangold");
        document.put("year", "2005");

        // Form the indexing request, send it, and print the response
        IndexRequest request = new IndexRequest(index, type, id).source(document);
        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }

    // Adds the interceptor to the ES REST client
    public static RestHighLevelClient esClient(String serviceName, String region) {
        /*AWS4Signer signer = new AWS4Signer();
        signer.setServiceName(serviceName);
        signer.setRegionName(region);
        HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);*/


        final CredentialsProvider provider = new BasicCredentialsProvider();

        provider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("us_jhao", "Tiktok123!"));
        return new RestHighLevelClient(RestClient.builder(HttpHost.create(aesEndpoint)).setHttpClientConfigCallback(x-> x.setDefaultCredentialsProvider(provider)));
    }

    public static class MyProvider implements AWSCredentialsProvider {
        public MyProvider() {
        }

        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials("AKIASTECR6IVM6CK6XYL ", "BP8mED/zd/AzqV25oyhy2umnQUFnt8lFVf5EnGQi");
        }

        public void refresh() {
        }
    }
}
